/*
包含n个action creator
异步action
同步action
 */
import {
  RECEIVE_PERSONALIZED_LIST,
  RECEIVE_RANKING_LIST,
  RECEIVE_BILLBOARD,
  RECEIVE_RECOMMEND,
  RECEIVE_SEARCH,
  RECEIVE_SEARCHHOT
} from "./action-types";
import {
  reqPersonalizedList,
  reqTopList,
  reqRecommend,
  reqSearch,
  reqSearchHot
} from "../api";

//获取推荐列表同步action
const receivePersonalizedList = personalizedList => ({
  type: RECEIVE_PERSONALIZED_LIST,
  data: personalizedList
});
//获取推荐列表同步action
const receiveRankingList = rankingObj => ({
  type: RECEIVE_RANKING_LIST,
  data: rankingObj
});
//获取榜单的具体信息
const receiveBillBoard = billBoard => ({
  type: RECEIVE_BILLBOARD,
  data: billBoard
});
//获取榜单的具体信息
const receiveRecommend = recommend => ({
  type: RECEIVE_RECOMMEND,
  data: recommend
});

//搜取音乐信息
const receiveSearch = searchResult => ({
  type: RECEIVE_SEARCH,
  data: searchResult
});
//获取音乐热词
const receiveSearchHot = searchHotResult => ({
  type: RECEIVE_SEARCHHOT,
  data: searchHotResult
});


export const getReset = () => {
  return dispatch => {
    dispatch(receiveSearch({
      songCount: 0,
      temArr: []
    }))
  };
};
//获取推荐列表信息
export const getPersonalizedList = () => {
  return async dispatch => {
    // 执行异步ajax请求
    const response = await reqPersonalizedList();
    const result = response.data;
    // debugger;
    // 得到结果后, 分发一个同步action
    if (result.code === 200) {
      dispatch(receivePersonalizedList(result.result));
    }
  };
};
//获取排行列表信息
export const getRankList = idxArr => {
  return dispatch => {
    // 执行异步ajax请求
    let rankingPromises = [];
    let rankingData = [];

    idxArr.map(item => rankingPromises.push(reqTopList(item)));
    Promise.all(rankingPromises)
      .then(ranks => {
        ranks.map((rank, index) => {
          let itemObj = rank.data.playlist;
          let temObj = {};
          temObj["index"] = index;
          temObj["name"] = itemObj.name;
          temObj["coverImgUrl"] = itemObj.coverImgUrl;
          var temArr = [];
          for (var t = 0; t < 3; t++) {
            var obj = itemObj.tracks[t];
            temArr.push({
              id: obj.id,
              name: obj.name
            });
          }
          temObj["tracks"] = temArr;
          rankingData.push(temObj);
        });
        // console.log(rankingData);
        dispatch(receiveRankingList(rankingData));
      })
      .catch(error => {
        console.log(error);
      });
  };
};
//获取具体榜单信息
export const getBillBoard = (billBoardId, type) => {
  return async dispatch => {
    // 执行异步ajax请求
    // debugger;
    let response, result;
    if (type == "B") {
      response = await reqTopList(billBoardId);
      result = response.data;
    } else if (type == "R") {
      response = await reqRecommend(billBoardId);
      result = response.data;
    }
    // debugger;
    // 得到结果后, 分发一个同步action
    if (result.code === 200) {
      let itemObj = result.playlist;
      let billBoard = {};
      billBoard["tracks"] = [];
      billBoard["name"] = itemObj.name;
      billBoard["coverImgUrl"] = itemObj.coverImgUrl;
      billBoard["updateTime"] = itemObj.updateTime;
      billBoard["tags"] = itemObj.tags || [];
      let temObj = itemObj["tracks"];
      for (let t = 0; t < 20; t++) {
        try {
          var obj = temObj[t];
          billBoard["tracks"].push({
            musicIndx: t || -1000,
            id: obj.id || "",
            title: obj.name || "",
            artist: obj.ar[0].name || "",
            src: "",
            lrc: ""
          });
        } catch (error) {}
      }
      dispatch(receiveBillBoard(billBoard));
    }
  };
};

export const getSearchResult = (keyword, offset, limit) => {
  return async dispatch => {
    // 执行异步ajax请求
    const response = await reqSearch(keyword, offset, limit);
    const result = response.data;
    // debugger;
    // 得到结果后, 分发一个同步action
    if (result.code === 200) {
      // debugger;
      try {
        let itemObj = result.result.songs;
        let songs = {};
        songs["songCount"] = result.result.songCount;
        songs["temArr"] = [];
        for (let t = 0; t < itemObj.length; t++) {
          var obj = itemObj[t];
          songs["temArr"].push({
            musicIndx: t,
            id: obj.id,
            artist: obj.artists[0] && obj.artists[0].name,
            artistsName: obj.artists[0] && obj.artists[0].name,
            albumName: obj.album && obj.album.name,
            img1v1Url: obj.artists[0] && obj.artists[0].img1v1Url,
            keyWord: obj.name,
            title: obj.album && obj.album.name,
            pic: obj.artists[0] && obj.artists[0].img1v1Url,
            lrc: "",
            src: ""
          });
        }
        dispatch(receiveSearch(songs));
      } catch (error) {
        console.log(error);
      }

    }
  };
};
export const getSearchHotResult = () => {
  return async dispatch => {
    // 执行异步ajax请求
    const response = await reqSearchHot();
    const result = response.data;
    // debugger;
    // 得到结果后, 分发一个同步action
    if (result.code === 200) {
      // debugger
      let itemObj = result.result.hots;
      let temArr = [];
      for (let t = 0; t < itemObj.length; t++) {
        let obj = itemObj[t];
        temArr.push({
          first: obj.first,
          second: obj.second,
          third: obj.third,
          iconType: obj.iconType
        });
      }
      dispatch(receiveSearchHot(temArr));
    }
  };
};